package com.yzcrm.common.packagemethod;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Record;
import com.yzcrm.common.config.paragetter.BasePageRequest;
import com.yzcrm.erp.admin.entity.AdminScene;
import com.yzcrm.erp.crm.common.QuotationCombEnum;
import org.apache.poi.ss.formula.functions.T;

import java.math.BigDecimal;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Flowers bloom all over the ground, frequent urination urination not enough!
 * IDE: Created with by IntelliJ IDEA.
 * program: yzcrm
 *
 * @author :jones
 * github: https://github.com/luckyrxp
 * email: jonesxr@163.com
 * version: v 1.0
 *
 * <p>
 * <p>
 * date: 2020-08-25
 * time: 15:18
 * description:封装的方法
 * Copyright(©) 2020 by Jones
 **/

public class packageMethods {

    /**
     * 抽取场景公共代码
     *
     * @param basePageRequest basePageRequest
     */
    public static void getEncapsulatingTheScene(BasePageRequest<T> basePageRequest) {
        JSONObject jsonObject = basePageRequest.getJsonObject();
        Integer sceneId = jsonObject.getInteger("sceneId");
        JSONObject data = new JSONObject();
        if (sceneId != null && sceneId != 0) {
            data = JSON.parseObject(AdminScene.dao.findById(sceneId).getData());
        }
        if (sceneId == null && jsonObject.getInteger("type") == 1) {
            data = new JSONObject()
                    .fluentPut("is_transform", new JSONObject()
                            .fluentPut("name", "is_transform").fluentPut("condition", "is").fluentPut("value", "0"));
        }
        if (jsonObject.getJSONObject("data") != null) {
            if (data != null) {
                data.putAll(jsonObject.getJSONObject("data"));
            }
        }
        jsonObject.put("data", data);
        basePageRequest.setJsonObject(jsonObject);
    }

    /**
     * 抽取分页设置公共代码
     *
     * @param basePageRequest basePageRequest
     * @param kv              map -> key,value
     * @param queryList       queryList
     * @param conditions      conditions
     */
    public static void setPageMethod(BasePageRequest<T> basePageRequest, Kv kv, List<JSONObject> queryList, StringBuilder conditions) {
        JSONObject sqlObject = new JSONObject();
        sqlObject.put("sql", conditions.toString());
        sqlObject.put("type", 1);
        if (!kv.containsKey("fixed")) {
            kv.set("fixed", true);
        }
        queryList.add(sqlObject);

        // 设置分页
        if (StrUtil.isEmpty(basePageRequest.getJsonObject().getString("excel"))) {
            kv.set("page", (basePageRequest.getPage() - 1) * basePageRequest.getLimit()).set("limit", basePageRequest.getLimit());
        }
    }

    public static void getMaterialOptionType(LinkedList<Record> resultList, Record r, List<Record> recordList) {
        // 封装不同产品方式的 data
        String product_rate = null;
        BigDecimal subtotal = null;
        List<Record> materialOption = recordList.stream().filter(m ->
                m.getInt("material_option") == QuotationCombEnum.ITEM_OPTION_HARDWARE.getCode())
                .collect(Collectors.toList());
        for (Record record : materialOption) {
            product_rate = record.getStr("vatRates");
            subtotal = record.getBigDecimal("subtotal");
        }
        if (materialOption.size() > 0) {
            resultList.add(new Record()
                    .set("rate", product_rate)
                    .set("itemOption", QuotationCombEnum.ITEM_OPTION_HARDWARE.getName())
                    .set("quotationItemId", QuotationCombEnum.ITEM_OPTION_HARDWARE.getCode())
                    .set("subtotal", subtotal)
                    .set("productList", materialOption));
        }

        List<Record> materialOption1 = recordList.stream().filter(m -> m.getInt("material_option") == QuotationCombEnum.ITEM_OPTION_AUTH.getCode())
                .collect(Collectors.toList());
        for (Record record : materialOption1) {
            product_rate = record.getStr("vatRates");
            subtotal = record.getBigDecimal("subtotal");
        }
        if (materialOption1.size() > 0) {
            resultList.add(new Record()
                    .set("rate", product_rate)
                    .set("itemOption", QuotationCombEnum.ITEM_OPTION_AUTH.getName())
                    .set("quotationItemId", QuotationCombEnum.ITEM_OPTION_AUTH.getCode())
                    .set("subtotal", subtotal)
                    .set("productList", materialOption1));
        }


        List<Record> materialOption2 = recordList.stream().filter(m -> m.getInt("material_option") == QuotationCombEnum.ITEM_OPTION_LOCAL_DEPLOY_AUTH.getCode())
                .collect(Collectors.toList());
        for (Record record : materialOption2) {
            product_rate = record.getStr("vatRates");
            subtotal = record.getBigDecimal("subtotal");
        }
        if (materialOption2.size() > 0) {
            resultList.add(new Record()
                    .set("rate", product_rate)
                    .set("itemOption", QuotationCombEnum.ITEM_OPTION_LOCAL_DEPLOY_AUTH.getName())
                    .set("quotationItemId", QuotationCombEnum.ITEM_OPTION_LOCAL_DEPLOY_AUTH.getCode())
                    .set("subtotal", subtotal)
                    .set("productList", materialOption2));
        }


        List<Record> materialOption3 = recordList.stream().filter(m -> m.getInt("material_option") == QuotationCombEnum.ITEM_OPTION_ANNUAL_FEE.getCode())
                .collect(Collectors.toList());
        for (Record record : materialOption3) {
            product_rate = record.getStr("vatRates");
            subtotal = record.getBigDecimal("subtotal");
        }
        if (materialOption3.size() > 0) {
            resultList.add(new Record()
                    .set("rate", product_rate)
                    .set("itemOption", QuotationCombEnum.ITEM_OPTION_ANNUAL_FEE.getName())
                    .set("quotationItemId", QuotationCombEnum.ITEM_OPTION_ANNUAL_FEE.getCode())
                    .set("subtotal", subtotal)
                    .set("productList", materialOption3));
        }


        List<Record> materialOption4 = recordList.stream().filter(m -> m.getInt("material_option") == QuotationCombEnum.ITEM_OPTION_ARTIFICIAL.getCode())
                .collect(Collectors.toList());
        for (Record record : materialOption4) {
            product_rate = record.getStr("vatRates");
            subtotal = record.getBigDecimal("subtotal");
        }
        if (materialOption4.size() > 0) {
            resultList.add(new Record()
                    .set("rate", product_rate)
                    .set("itemOption", QuotationCombEnum.ITEM_OPTION_ARTIFICIAL.getName())
                    .set("quotationItemId", QuotationCombEnum.ITEM_OPTION_ARTIFICIAL.getCode())
                    .set("subtotal", subtotal)
                    .set("productList", materialOption4));
        }
    }

}
